package com.lh.leetcodetop100;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: linghao
 * @since: 2022/10/25
 */
public class 两数之和_1 {

    public int[] twoSum(int[] nums, int target) {
        int[] ret = new int[2];
        int len = nums.length;
        for(int i=0;i<len;i++){
            for(int j=i+1;j<len;j++){
                if((nums[i]+nums[j]) == target){
                    ret[0] = i;
                    ret[1] = j;
                }
            }
        }
        return ret;
    }

    public int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }

}
